﻿using Aliphant.Common.Tools;
using CommonLibrary.Helper;
using DataModel;
using DataModel.Contants;
using DataModel.Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CuteWallpaperDemo.Managers
{
    public class ConfigManager : Singleton<ConfigManager>
    {
        public bool IsEdit = true;
        public SettingConfig Settings = new SettingConfig();
        public PlayList PlayList = new PlayList();
        public PlayList WallpaperIndex = new PlayList();

        public void Init()
        {
            LoadSettings();
            LoadWallpaperIndex();
            LoadPlaylist();
        }

        private void LoadSettings()
        {
            string path = Path.Combine(DomainHelper.GetDirectoryPath(DirectoryDefine.CONFIG), AppDefines.Config + ".json");
            string res = JsonHelper.ReadJson(path, out SettingConfig? sc, null);
            if (!string.IsNullOrEmpty(res))
            {
                //没有配置文件
                Settings = new SettingConfig();
                JsonHelper.SaveToJson(ref Settings, path);
            }
            else if (sc != null)
            {
                Settings = sc;
            }
        }

        private void LoadPlaylist()
        {
            string path = Path.Combine(DomainHelper.GetDirectoryPath(DirectoryDefine.CONFIG), AppDefines.Playlist + ".json");
            string res = JsonHelper.ReadJson(path, out PlayList? pl, null);
            if (!string.IsNullOrEmpty(res))
            {
                //没有配置文件
                PlayList = new PlayList();
                JsonHelper.SaveToJson(ref Settings, path);
            }
            else if (pl != null)
            {
                PlayList = pl;
            }
        }

        private void LoadWallpaperIndex()
        {
            string path = Path.Combine(DomainHelper.GetDirectoryPath(DirectoryDefine.CONFIG), AppDefines.Wallpaperindex + ".json");
            string res = JsonHelper.ReadJson(path, out PlayList? wi, null);
            if (!string.IsNullOrEmpty(res))
            {
                //没有配置文件
                WallpaperIndex = new PlayList();
                JsonHelper.SaveToJson(ref Settings, path);
            }
            else if (wi != null)
            {
                WallpaperIndex = wi;
            }
        }

        public void Save()
        {
            if (!IsEdit) return;

            string path = Path.Combine(DomainHelper.GetDirectoryPath(DirectoryDefine.CONFIG), AppDefines.Config + ".json");
            JsonHelper.SaveToJson(ref Settings, path);

            path = Path.Combine(DomainHelper.GetDirectoryPath(DirectoryDefine.CONFIG), AppDefines.Playlist + ".json");
            JsonHelper.SaveToJson(ref PlayList, path);

            path = Path.Combine(DomainHelper.GetDirectoryPath(DirectoryDefine.CONFIG), AppDefines.Wallpaperindex + ".json");
            JsonHelper.SaveToJson(ref WallpaperIndex, path);
        }
    }
}
